package com.tt.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tt.pojo.Address;
import com.tt.pojo.Info;
import com.tt.pojo.User;
import com.tt.service.AddressService;
import com.tt.service.InfoService;
import com.tt.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    InfoService infoService;
    @Autowired
    AddressService addressService;

    @RequestMapping(value = "/toInfoList.html")
    public String toMessageInfo(){
        return "/admin/infoList";
    }

    @RequestMapping(value = "/toAddress.html")
    public String toAddress(){
        return "/admin/addressList";
    }

    @RequestMapping("/toAddInfo.html")
    public String toAddInfo(HttpServletRequest request){
        List<Address> addresses = addressService.getAddressList(null);
        request.setAttribute("didian",addresses);
        return "admin/addInfo";
    }


    @RequestMapping("/getAddressList.html")
    @ResponseBody
    public PageInfo<Address> addressList(Address address, @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        System.out.println(pageNum);

        PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<Address> addresses = addressService.getAddressList(address);
        PageInfo<Address> pageInfo = new PageInfo<Address>(addresses);
        return pageInfo;
    }

    @RequestMapping("/getInfoList.html")
    @ResponseBody
    public PageInfo<Info> infoList(Info info, @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        System.out.println(pageNum);

        PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<Info> infos = infoService.getInfoList(info);
        PageInfo<Info> pageInfo = new PageInfo<Info>(infos);
        return pageInfo;
    }


    @RequestMapping("/toGetAddress.html")
    public String toAddDorm(HttpServletRequest request){
            List<Address> addresses = addressService.getAddressList(null);
            request.setAttribute("didian",addresses);
            return "admin/addEnroll";
    }

    //去修改地址
    @RequestMapping("to_updateAddress.html")
    public String toUpdateNotice(Integer id,HttpServletRequest request){
        Address address=addressService.getAddressById(id);
        request.setAttribute("didian",address);
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
            return "admin/updateAddress";
    }
    @RequestMapping("updateAddress.html")
    public String updateNotice(Address address, HttpServletRequest request) {
        int i = addressService.updateAddress(address);
        if(i>0){
            request.setAttribute("bs", "修改公告信息成功");
        }else{
            request.setAttribute("bs", "修改公告信息失败");
        }

        return "redirect:to_updateAddress.html";
    }

    @RequestMapping("addInfo.html")
    @ResponseBody
    public String addInfo(Info info) {
        int i =infoService.addInfo(info);
        if(i>0){
            return "success";
        }else
        {
            return "添加失败";
        }

    }




}
